from django.db import models
from users.models import *
from goods.models import *
# Create your models here.
class Addr(models.Model):
    receiver = models.CharField('收件人',max_length=20)
    receiver_mobile = models.CharField('手机号',max_length=11)
    receiver_addr = models.CharField('收货地址',max_length=100)
    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='当前用户')
    def __str__(self):
        return "用户%s的收货地址"%self.user.username
    class Meta:
        db_table = 'addr'
        verbose_name_plural = '收货地址'
class OrderInfo(models.Model):
    #支付方式的选项
    PAY_METHOD = (
        (1,'支付宝'),
        (2,'微信支付'),
        (3,'银联支付'),
        (4,'货到付款')
    )
    #支付状态
    PAY_STATUS = (
        (1,'待支付'),
        (2,'待发货'),
        (3,'待收货'),
        (4,'待评价'),
        (5,'以完成')
    )
    order_id = models.CharField('订单编号',max_length=64,primary_key=True)
    user = models.ForeignKey(User,on_delete=models.CASCADE,
                             verbose_name='下单用户',related_name='orders')
    addr = models.ForeignKey(Addr,on_delete=models.CASCADE,verbose_name='收货地址')
    total_count = models.IntegerField('商品总数',default=0)
    total_amount = models.DecimalField('总价格',max_digits=10,decimal_places=2)
    freight = models.DecimalField('运费',max_digits=10,decimal_places=2)
    pay_method = models.SmallIntegerField('支付方式',default=1,choices=PAY_METHOD)
    status = models.SmallIntegerField('支付状态',choices=PAY_STATUS)
    create_time = models.DateTimeField('创建时间',auto_now_add=True)
    def __str__(self):
        return "用户%s:订单%s"%(self.user.username,self.order_id)
    class Meta:
        db_table = 'order_info'
        verbose_name_plural = '订单信息'

class OrderGoods(models.Model):
    #评分选项
    SCORES = (
        (1,"20分"),
        (2,'40分'),
        (3,'60分'),
        (4,'80分'),
        (5,'100分')
    )
    sku = models.ForeignKey(SKU,on_delete=models.CASCADE,verbose_name='订单商品')
    count = models.IntegerField('购买数量',default=1)
    price = models.DecimalField('价格',max_digits=10,decimal_places=2)
    order = models.ForeignKey(OrderInfo,on_delete=models.CASCADE,
                              verbose_name="所属订单",related_name='order_goods')
    comment = models.TextField('评论信息',default="")
    score = models.SmallIntegerField('评分',default=5,choices=SCORES)
    is_anonymous = models.BooleanField('是否匿名评价',default=False)
    is_commented = models.BooleanField('是否评价',default=False)
    def __str__(self):
        return "订单%s:商品%s"%(self.order.order_id,self.sku.name)
    class Meta:
        db_table = 'order_goods'
        verbose_name_plural = '订单商品'

class Payment(models.Model):
    trade_id = models.CharField('支付编码',max_length=100,unique=True,null=True,blank=True)
    order = models.ForeignKey(OrderInfo,on_delete=models.CASCADE,verbose_name='订单')
    def __str__(self):
        return "订单%s：订单编号%s"%(self.order.order_id,self.trade_id)
    class Meta:
        db_table = 'payment'
        verbose_name_plural = '支付信息'



